package com.lg.web;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.lg.dao.UserInfoRepository;
import com.lg.entiry.UserInfo;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
	@Autowired
	private UserInfoRepository userInfoRepository;
    /**
     * 用户查询.
     * @return
     */
    @RequestMapping("/userList")
    @RequiresPermissions("userInfo:view")//权限管理;
    public String userInfo(Map<String ,Object> map){
    	 List<UserInfo> findAll = userInfoRepository.findAll();
         map.put("users", findAll);
        return "userInfo";
    }

    /**
     * 用户添加;
     * @return
     */
    @RequestMapping(value="/userAdd",method=RequestMethod.GET)
    @RequiresPermissions("userInfo:add")//权限管理;
    public String userInfoAdd(){
        return "userInfoAdd";
    }
    /**
     * 用户添加;
     * @return
     */
    @RequestMapping(value="/userAdd",method=RequestMethod.POST)
    @RequiresPermissions("userInfo:add")//权限管理;
    public String userInfoAdd(UserInfo userInfo){
    	String password = userInfo.getPassword();
    	String salt = userInfo.getSalt();
    	 SimpleHash simpleHash = new SimpleHash(new Md5Hash().getAlgorithmName(), password,  ByteSource.Util.bytes(userInfo.getCredentialsSalt()), 1);
    	 userInfo.setPassword(simpleHash.toString());
    	 return "userInfoAdd";
    }
    /**
     * 用户删除;
     * @return
     */
    @RequestMapping("/userDel")
    @RequiresPermissions("userInfo:del")//权限管理;
    public String userDel(@RequestParam("uid") Long uid){
    	userInfoRepository.deleteById(uid);
        return "index";
    }
}